package problem.nc016;

import sun.reflect.generics.tree.Tree;

public class Solution {


    static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;

        }
    }
    /*
    *   一个链表是否对称
    * */
    public boolean isSymmetrical(TreeNode pRoot) {
        if(pRoot == null){
            return true;
        }
        return isSameTree(jiangxiang(pRoot.left),pRoot.right);
    }
    /*
    *   求一个树的镜像
    * */
    public TreeNode jiangxiang(TreeNode root){
        if(root == null){
            return null;
        }

        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        jiangxiang(root.left);
        jiangxiang(root.right);
        return root;
    }

    /*
    *   判断两个树是否相同
    * */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }

        if(p == null || q == null){
            return false;
        }

        if(p.val != q.val){
            return false;
        }

        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
}
